package com.ruoyi.hospital.department.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.hospital.department.domain.TDepartment;
import com.ruoyi.hospital.department.mapper.TDepartmentMapper;
import com.ruoyi.hospital.department.service.ITDepartmentService;
import com.ruoyi.hospital.hospitalized.domain.THospitalized;
import com.ruoyi.hospital.hospitalized.mapper.THospitalizedMapper;
import com.ruoyi.hospital.registered.domain.TRegistered;
import com.ruoyi.hospital.registered.mapper.TRegisteredMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 科室管理Service业务层处理
 *
 * @author Shawn
 * @date 2024-02-20
 */
@Service
public class TDepartmentServiceImpl implements ITDepartmentService {
    @Autowired
    private TDepartmentMapper tDepartmentMapper;

    @Autowired
    private TRegisteredMapper tRegisteredMapper;

    @Autowired
    private THospitalizedMapper tHospitalizedMapper;

    /**
     * 查询科室管理
     *
     * @param id 科室管理主键
     * @return 科室管理
     */
    @Override
    public TDepartment selectTDepartmentById(Long id) {
        return tDepartmentMapper.selectTDepartmentById(id);
    }

    /**
     * 查询科室管理列表
     *
     * @param tDepartment 科室管理
     * @return 科室管理
     */
    @Override
    public List<TDepartment> selectTDepartmentList(TDepartment tDepartment) {
        return tDepartmentMapper.selectTDepartmentList(tDepartment);
    }

    /**
     * 新增科室管理
     *
     * @param tDepartment 科室管理
     * @return 结果
     */
    @Override
    public int insertTDepartment(TDepartment tDepartment) {
        List<TDepartment> tDepartmentList = tDepartmentMapper.selectTDepartmentByName(tDepartment.getName());
        if (!tDepartmentList.isEmpty()) {
            throw new ServiceException("科室不允许重复");
        }
        tDepartment.setCreatedTime(new Date());
        tDepartment.setCreatedBy(String.valueOf(ShiroUtils.getUserId()));
        return tDepartmentMapper.insertTDepartment(tDepartment);
    }

    /**
     * 修改科室管理
     *
     * @param tDepartment 科室管理
     * @return 结果
     */
    @Override
    public int updateTDepartment(TDepartment tDepartment) {
        return tDepartmentMapper.updateTDepartment(tDepartment);
    }

    /**
     * 批量删除科室管理
     *
     * @param ids 需要删除的科室管理主键
     * @return 结果
     */
    @Override
    public int deleteTDepartmentByIds(String ids) {
        String[] array = Convert.toStrArray(ids);
        for (String id : array) {
            checkDelete(id);
        }
        return tDepartmentMapper.deleteTDepartmentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除科室管理信息
     *
     * @param id 科室管理主键
     * @return 结果
     */
    @Override
    public int deleteTDepartmentById(Long id) {
        return tDepartmentMapper.deleteTDepartmentById(id);
    }


    private void checkDelete(String departmentId) {
        TRegistered tRegistered = new TRegistered();
        tRegistered.setDepartmentId(departmentId);
        List<TRegistered> tRegisteredList = tRegisteredMapper.selectTRegisteredList(tRegistered);
        if (!tRegisteredList.isEmpty()) {
            throw new ServiceException("已有门诊引用，不能删除医生");
        }
        THospitalized tHospitalized = new THospitalized();
        tHospitalized.setDepartmentId(departmentId);
        List<THospitalized> tHospitalizedList = tHospitalizedMapper.selectTHospitalizedList(tHospitalized);
        if (!tHospitalizedList.isEmpty()) {
            throw new ServiceException("已有住院管理引用，不能删除医生");
        }
    }
}
